﻿using System.IO;
using System.Runtime.Serialization;


namespace Rodunu.Clients.WPF
{
    public class Serializer<T>
    {
        public void SerializeToFile(T items, string filename)
        {
            var contractSerializer = new DataContractSerializer(typeof(T));
            using (Stream s = File.Create(filename))
                contractSerializer.WriteObject(s, items);  
        }

        private static T DeserializeFromFile(string filename)
        {
            var contractSerializer = new DataContractSerializer(typeof(T));
            if (!File.Exists(filename))
            {
                File.Create(filename);
            }
            using (Stream s = File.OpenRead(filename))
                return (T)contractSerializer.ReadObject(s); 
        }

        public T Deserialize(string FileName)
        {
            return DeserializeFromFile(FileName);
        }
    }
}